package com.situ.blog.service.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.dao.impl.BlogDaoImpl;
import com.situ.blog.listener.BlogExcelListener;
import com.situ.blog.pojo.entity.Blog;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.pojo.vo.BlogVO;
import com.situ.blog.service.IBlogService;
import com.situ.blog.util.ExcelUtils;
import com.situ.blog.util.PageInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class BlogServiceImpl implements IBlogService {
    private IBlogDao blogDao = new BlogDaoImpl();

    @Override
    public void update(Blog blog) {
        blogDao.update(blog);
    }

    @Override
    public Blog selectById(int id) {
        return blogDao.selectById(id);
    }

    @Override
    public void add(Blog blog) {
        blogDao.add(blog);
    }

    //批量删除:通过遍历删除单个元素
    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            //把数组赋值给字符串
            //把字符串转为整数
            blogDao.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void deleteById(int id) {
        blogDao.deleteById(id);
    }
    //分页查询
    //由于页面有搜索功能，所以分页需要再搜索的后面，
    //只有确定了搜索目标，才能根据搜索目标的个数来分页
    @Override
    public PageInfo selectByPage(BlogQuery blogQuery) {
        //两次调用dao
        //第一次调用dao:当前页数据
        List<BlogVO> list = blogDao.selectByPage(blogQuery);
        //第二次调用dao:总数量
        int totalCount = blogDao.selectTotalCount(blogQuery);

        return PageInfo.ok(totalCount,list);
    }

    @Override
    public void updateStatus(int id, int status) {
        blogDao.updateStatus(id,status);
    }

    @Override
    public List<BlogVO> selectBlogCount(BlogQuery blogQuery) {
        return blogDao.selectBlogCount(blogQuery);
    }

    @Override
    public void exportExcel(HttpServletResponse resp) {
        List<BlogVO> list = blogDao.selectAll();
        ExcelUtils.exportExcel(resp,list,BlogVO.class,"blog数据");
    }

    @Override
    public void importExcel(HttpServletRequest req) {
        ExcelUtils.importExcel(req,new BlogExcelListener(),BlogVO.class);
    }
}
